package org.alex.samples.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.alex.samples.model.Subject;
import org.alex.samples.model.SubjectVO;
import org.alex.samples.service.ISubjectService;
import org.alex.samples.service.ManageTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
@RequestMapping({ "/subject" })
public class SubjectController {

	@Autowired
	ISubjectService subjectService;
	
	@Autowired
	public ManageTestService manageTestService;
	

	@RequestMapping({ "/subjects" })
	public String findAllSubject(Model model) throws Exception {
		List<SubjectVO> subjects =  subjectService.findAllSubject();
		model.addAttribute("subjectList", subjects);
		return "subject/subjectList";
	}

	@ResponseBody
	@RequestMapping(value = { "/add" }, method = { RequestMethod.POST })
	private Map<String,Object> addSubject(Subject subject)  {
		Map<String,Object> map=new HashMap<String,Object>();		
		map.put("success", false);
		try{
			this.subjectService.createSubject(subject);
			map.put("success", true);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	//修改科目信息
	@ResponseBody
	@RequestMapping(value = { "/edit" }, method = { RequestMethod.POST })
	private Map<String,Object> editSubject(Subject subject) throws Exception {
		Map<String,Object> map=new HashMap<String,Object>();		
		map.put("success", false);
		try{
			this.subjectService.EditSubject(subject);
			map.put("success", true);
		}catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	@RequestMapping(value = { "/del/{id}" }, method = { RequestMethod.GET })
	private String deleteSubjectDelete(RedirectAttributes redirectAttributes, @PathVariable Integer id) throws Exception {
		this.subjectService.deleteSubject(id);
		return "redirect:/subject/subjects";
	}

	//查找所有科目
	@ResponseBody
	@RequestMapping(value={ "/showSubjects" },method = { RequestMethod.POST,RequestMethod.GET })
	public List<SubjectVO> showSubjects() throws Exception {
		List<SubjectVO> subjects = null;
		try {
			 subjects = this.subjectService.findAllSubject();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return subjects;
	}
	
}